package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.*;
import com.qy.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.qy.service.PolicyOrderService;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("/app/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private PolicyOrderService policyOrderService;

    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        commentService.save(comment);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        commentService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Comment comment) {
        commentService.update(comment);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Comment comment = commentService.findById(id);
        return ResultGenerator.successResult(comment);
    }

    @GetMapping("/list")
    public Result list(PageBean<Comment> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Comment> list = commentService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //网友印象
    @GetMapping("/netFriendImpress")
    public Result netFriendImpress(Comment comment) {
        List<Map> list = new ArrayList<>();
        if (comment.getPolicy_id() != null) {
            //查询政策评论标签列表
            list = commentService.selectPolicyCommentLabel(comment);
        } else if (comment.getUnit_id() != null) {
            //查询单位评论标签列表
            list = commentService.selectUnitCommentLabel(comment);
        } else if (comment.getCharge_id() != null) {
            //查询负责人评论标签列表
            list = commentService.selectChargeCommentLabel(comment);
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }

        return ResultGenerator.successResult(list);
    }

    //评论列表
    @GetMapping("/commentList")
    public Result commentList(PageBean<Map> page, Comment comment) {
        if (comment.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (comment.getLabel() == null || comment.getLabel().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        List<Map> list = new ArrayList<>();
        if (comment.getPolicy_id() != null) {
            //查询政策评论列表
            PageHelper.startPage(page.getPageNum(), page.getSize(), "likeNumber desc,add_time desc");
            list = commentService.selectPolicyCommentList(comment);
        } else if (comment.getUnit_id() != null) {
            //查询单位评论列表
            PageHelper.startPage(page.getPageNum(), page.getSize(), "likeNumber desc,add_time desc");
            list = commentService.selectUnitCommentList(comment);
        } else if (comment.getCharge_id() != null) {
            //查询负责人评论列表
            PageHelper.startPage(page.getPageNum(), page.getSize(), "likeNumber desc,add_time desc");
            list = commentService.selectChargeCommentList(comment);
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        page.setList(list);
        return ResultGenerator.successResult(page);

    }

    //评价
    @PostMapping("/comment")
    public Result comment(@RequestBody Comment comment) {
        if (comment.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }

        Condition condition = new Condition(PolicyOrder.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("user_id =" + comment.getUser_id());
        if (comment.getPolicy_id() != null) {
            criteria.andCondition("policy_id =" + comment.getPolicy_id());
        } else if (comment.getUnit_id() != null) {
            criteria.andCondition("unit_id =" + comment.getUnit_id());
        } else if (comment.getCharge_id() != null) {
            criteria.andCondition("charge_id =" + comment.getCharge_id());
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<PolicyOrder> list = policyOrderService.findByCondition(condition);
        PolicyOrder policyOrder = list.get(0);

        if (comment.getScore() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (comment.getLabel() == null || comment.getLabel().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (comment.getContent() == null || comment.getContent().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }

        comment.setOrder_id(policyOrder.getId());
        comment.setOrder_number(policyOrder.getOrder_number());
        comment.setName(policyOrder.getName());
        comment.setPhone_number(policyOrder.getPhone());
        comment.setAdd_time(DateUtil.getNowTimestamp());
        commentService.save(comment);

        return ResultGenerator.successResult();
    }
}
